package com.perdragon.controller;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.perdragon.entity.Educate;
import com.perdragon.service.EducateService;

@Controller
@RequestMapping("/educate")
public class EducateController {
	@Autowired
	private EducateService educateService;

	@InitBinder
	public void initBinder(WebDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	}

	@RequestMapping("/list")
	public ModelAndView getList(String page) {
		int pageNum = 1;
		if (page != null)
			pageNum = Integer.parseInt(page);
		int rowNum = educateService.getRowNum();
		int maxPageNum = (int) Math.ceil(rowNum / 10.0);
		if (maxPageNum == 0)
			maxPageNum = 1;
		if (pageNum >= 1 && pageNum <= maxPageNum) {
			ModelAndView mv = new ModelAndView();
			mv.setViewName("educate/listEducate");
			mv.addObject("educateList", educateService.listEducate((pageNum - 1) * 10));
			mv.addObject("pageNum", pageNum);
			mv.addObject("maxPageNum", maxPageNum);
			return mv;
		} else {
			ModelAndView mv = new ModelAndView("redirect:/educate/list");
			return mv;
		}
	}

	@RequestMapping("/listFinished")
	public ModelAndView getFinishedList(String page) {
		int pageNum = 1;
		if (page != null)
			pageNum = Integer.parseInt(page);
		int rowNum = educateService.getRowNumFinished();
		int maxPageNum = (int) Math.ceil(rowNum / 10.0);
		if (maxPageNum == 0)
			maxPageNum = 1;
		if (pageNum >= 1 && pageNum <= maxPageNum) {
			ModelAndView mv = new ModelAndView();
			mv.setViewName("educate/listEducateFinished");
			mv.addObject("educateFinishedList", educateService.listEducateFinished((pageNum - 1) * 10));
			mv.addObject("pageNum", pageNum);
			mv.addObject("maxPageNum", maxPageNum);
			return mv;
		} else {
			ModelAndView mv = new ModelAndView("redirect:/educate/listFinished");
			return mv;
		}
	}

	@GetMapping("/add")
	public ModelAndView addEducate() {
		ModelAndView mv = new ModelAndView("educate/addEducate");
		return mv;
	}

	@PostMapping("/add")
	public ModelAndView doAddEducate(Educate educate) {
		Date createtime = new Date(System.currentTimeMillis());
		educate.setCreatetime(createtime);
		educateService.addEducate(educate);
		ModelAndView mv = new ModelAndView("redirect:/educate/list");
		return mv;
	}

	@RequestMapping("/detail")
	public ModelAndView showDetail(int id) {
		ModelAndView mv = new ModelAndView("educate/detailEducate");
		mv.addObject("educate", educateService.selectById(id));
		return mv;
	}

	@RequestMapping("/update")
	public ModelAndView updateEducate(int id) {
		ModelAndView mv = new ModelAndView("educate/updateEducate");
		mv.addObject("educate", educateService.selectById(id));
		return mv;
	}

	@PostMapping("/finished")
	public ModelAndView doUpdateEducate(Educate educate) {
		educateService.toFinished(educate);
		ModelAndView mv = new ModelAndView("redirect:/educate/listFinished");
		return mv;
	}

	@RequestMapping("/delete")
	public ModelAndView deleteEducate(int id) {
		educateService.delete(id);
		ModelAndView mv = new ModelAndView("redirect:/educate/list");
		return mv;
	}

	@RequestMapping("/deleteFinished")
	public ModelAndView deleteFinished(int id) {
		educateService.delete(id);
		ModelAndView mv = new ModelAndView("redirect:/educate/listFinished");
		return mv;
	}
}
